// 应用程序的根模块
import { HttpModule } from '@nestjs/axios';
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { JwtModule } from '@nestjs/jwt';
import { ScheduleModule } from '@nestjs/schedule';
import { SequelizeModule } from '@nestjs/sequelize';
import { ServeStaticModule } from '@nestjs/serve-static';
import { TerminusModule } from '@nestjs/terminus';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { join } from 'path';
import { ApiInterceptor } from './api.interceptor';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthGuard } from './auth.guard';
import { CatsController } from './cats/cats.controller';
import { CatsModule } from './cats/cats.module';
import { CatsService } from './cats/cats.service';
import { setupConfig } from './config';
import { HealthController } from './health/health.controller';
import { ApiMiddleware } from './middlewares/api.middleware';
import { LoggerMiddleware } from './middlewares/logger.middleware';
import { setupModules } from './models';
import { TasksModule } from './task/tasks.module';
import { UserController } from './user/user.controller';
import { UserService } from './user/user.service';
import { OpenController } from './open/open.controller';
import { ExampleController } from './manage/example/example.controller';
import { CdkeyController } from './manage/cdkey/cdkey.controller';
import { CommandController } from './command/command.controller';

@Module({
  imports: [
    setupConfig(),
    setupModules(),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '../client')
    }),
    JwtModule.registerAsync({
      global: true,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory(configService: ConfigService) {
        return {
          secret: configService.get<string>('JWT_SECRET'),
          signOptions: { expiresIn: configService.get<string>('JWT_EXPIRES_IN') }
        };
      }
    }),
    ThrottlerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory(configService: ConfigService) {
        return [{ ttl: configService.get<number>('API_THROTTLE_TIME')!, limit: configService.get<number>('API_THROTTLE_LIMIT')! }];
      }
    }),
    ScheduleModule.forRoot(),
    TasksModule,
    TerminusModule,
    HttpModule,
    CatsModule
  ],
  controllers: [AppController, UserController, CatsController, HealthController, OpenController, ExampleController, CdkeyController, CommandController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: ApiInterceptor
    },
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard
    },
    {
      provide: APP_GUARD,
      useClass: AuthGuard
    },
    AppService,
    UserService,
    CatsService
  ],
  exports: [SequelizeModule]
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(ApiMiddleware).forRoutes({ path: '/api/*', method: RequestMethod.ALL });
    consumer.apply(LoggerMiddleware).forRoutes({ path: '/api/cats', method: RequestMethod.GET });
  }
}
